local DIY = require "packages/diy_utility/diy_utility"

local yingong = fk.CreateSkill{
  name = "dajubei__yingong",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["dajubei__yingong"] = "引弓",
  [":dajubei__yingong"] = "锁定技，一名角色的回合内，每有一张牌进入弃牌堆，你的攻击范围便+1，然后若所有其他角色均在你攻击范围内，你可使用中央区内的一张【杀】，然后你不能发动此技能直到你回合开始。",

  ["@dajubei__yingong-turn"]= "引弓",
  ["#dajubei__yingong-use"]= "引弓：你可以使用中央区的一张【杀】",

  ["$dajubei__yingong1"] = "嘎啦嘎啦",
}

yingong:addAcquireEffect(function (self, player)
  player.room:addSkill("#CenterArea")
end)

yingong:addEffect(fk.AfterCardsMove, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player:getMark("@@dajubei__yingong") == 0 then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        n = n + #move.moveInfo
      end
    end
    room:addPlayerMark(player, "@dajubei__yingong-turn", n)
    if table.every(room:getOtherPlayers(player), function (p)
      return player:inMyAttackRange(p) end) and #DIY.getCenterCards(room) > 0 then
      local cards = table.filter(DIY.getCenterCards(room), function (id) return Fk:getCardById(id).trueName == "slash" end)
      local use = room:askToUseRealCard(player, {
        pattern = cards,
        skill_name = yingong.name,
        prompt = "#dajubei__yingong-use",
        extra_data = { expand_pile = cards },
        skip = true,
      })
      if use then
        room:invalidateSkill(player, yingong.name, nil, yingong.name)
        room:useCard(use)
      end
    end
  end,
})

yingong:addEffect("atkrange", {
  correct_func = function (self, from, to)
    return from:getMark("@dajubei__yingong-turn")
  end,
})

yingong:addEffect(fk.BeforeTurnStart, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(yingong.name, true)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:validateSkill(player, yingong.name, nil, yingong.name)
  end,
})

return yingong